
const adminModel=require('../models/adminModel')
const utils=require('../utils/index')

const myGlobal ={}


//显示所有菜单信息
exports.menuPage=(req,res)=>{
    myGlobal.path = req.originalUrl;
    adminModel.getAllMenu().then(function(data){
        // console.log(data)
       
        data=utils.arrFormat(data);
        res.render('admin/menu',{menus:data,path:myGlobal.path,user:req.session.user})
    })
}

//新增菜单
exports.addMenuPage=(req,res)=>{
    res.render('admin/menu_add',{path:myGlobal.path,user:req.session.user})
}
//提交新增信息
exports.addMenu=(req,res)=>{
    adminModel.addMenu(req.body).then(function(data){
        if(data.affectedRows>0){
            res.redirect('/admin/menu')
        }
    })
}
//编辑菜单
exports.editMenuPage=(req,res)=>{
    myGlobal.path = req.originalUrl;
    adminModel.getOneMenu(req.params.id).then(function(data){
        //  console.log('8888')
        //  console.log(req.params.id)
    //    console.log(data);
        res.render('admin/menu_edit',{menu:data[0],path:myGlobal.path,user:req.session.user})
  })
}
//提交编辑菜单
exports.editMenu=(req,res)=>{
    adminModel.editMenu(req.body).then(function(data){
        res.redirect('/admin/menu')
    })
}
//删除菜单
exports.deleteMenu=(req,res)=>{
    adminModel.deleteMenu(req.params.id).then(function(data){
        res.redirect('/admin/menu')
    })
}
//--------------------------文章分类----------------

//显示页面
exports.classPage=(req,res)=>{
    myGlobal.path = req.originalUrl;
    adminModel.getAllClass().then(function(data){
        data=utils.arrFormat(data);
        res.render('admin/class',{allClass:data,path:myGlobal.path,user:req.session.user})
    })
}
//添加文章
exports.addClassPage=(req,res)=>{
    res.render('admin/class_add',{path:myGlobal.path,user:req.session.user})
}

//提交添加的文章
exports.addClass=(req,res)=>{
    adminModel.addClass(req.body).then(function(data){
        res.redirect('/admin/class')
    })
}

//编辑文章：第一步
exports.editClassPage=(req,res)=>{
    // console.log(req.params.id)
    adminModel.getOneClass(req.params.id).then(function(data){
        res.render('admin/class_edit',{type:data[0],path:myGlobal.path,user:req.session.user})
    })
}

//编辑文章：第二部
exports.editClass=(req,res)=>{
    adminModel.editClass(req.body).then(function(data){
        res.redirect('/admin/class')
    })
}

//删除文章
exports.deleteClass=(req,res)=>{
    adminModel.deleteClass(req.params.id).then(function(data){
        res.redirect('/admin/class')
})
}




// ----------------------------------------------娴后台首页
exports.index = function(req, res) {
    adminModel.getNewArtical().then(function(data) {
        console.log(data);
        data = utils.arrFormat(data);
        myGlobal.path = req.originalUrl;
        res.render('admin/home', { user: req.session.user, path: myGlobal.path, newlist: data })
    })
}

// --------------文章管理--------------------
exports.articlePage = (req, res) => {
    myGlobal.path = req.originalUrl;
    adminModel.getAllAticles(req.query.page).then(data => {
        console.log(data[1][0].total);
        res.render('admin/artical', { user: req.session.user, path:myGlobal.path, articles: utils.arrFormat(data[0]), total: data[1][0].total });
    })
}
exports.articleAddPage = (req, res) => {
    adminModel.getAllClasses().then(data => {
        console.log(data);
        res.render('admin/artical_add', {user: req.session.user, path:myGlobal.path,classNames: data });
    })
}
exports.articleAdd = (req, res) => {
    req.body.userId = 1;
    adminModel.updateArticle(req.body).then(data => {
        res.redirect('/admin/article');
    })
}
exports.articleEditPage = (req, res) => {
        if (req.params.id == 'favicon.ico') return;
        console.log(req.params.id);
        myGlobal.path =req.originalUrl;
        adminModel.getOneEditArtical(req.params.id).then(data => {
            console.log(data);

            res.render('admin/artical_edit', {user: req.session.user, path:myGlobal.path, artical: data[0][0], classes: data[1] });
        })
}

// -----------------轮播图模块----------------

exports.getAllBanner=(req,res)=>{
    myGlobal.path =req.originalUrl;
    adminModel.getAllBanner().then((data)=>{   
        data=utils.arrFormat(data)
        res.render('admin/banner',{user: req.session.user, path:myGlobal.path,banners:data})
    })  
}

exports.addBannerPage = (req, res) => {

    res.render('admin/banner_add',{user: req.session.user, path:myGlobal.path});
}
exports.addBanner = (req, res) => {
    req.body.imgName = req.file.filename;
    adminModel.addBanner(req.body).then((data) => {
        if (data.affectedRows > 0) {
            res.redirect('/admin/banner')
        }
    })
}
exports.editBannerPage = (req, res) => {
    myGlobal.path =req.originalUrl;
    adminModel.editBannerPage(req.params.id).then((data) => {
    myGlobal.oldImgName = data[0].imgName;
    res.render('admin/banner_edit', { banner: data[0],user: req.session.user, path:myGlobal.path })
    })
}
exports.editBanner = (req, res) => {
    if (req.file) {
        req.body.imgName = req.file.filename;
        if (myGlobal.oldImgName) {
            utils.deleteFile(myGlobal.oldImgName)
        }
    }
    adminModel.editBanner(req.body).then(function(data) {
        if (data.affectedRows > 0) {
            res.redirect('/admin/banner')
        }

    })
}
exports.deleteBanner = (req, res) => {
    adminModel.deleteBanner(req.params.id).then((data) => {
        if (data.affectedRows > 0) {
            res.redirect('/admin/banner')
        }

    })
}



exports.loginPage = function(req, res) {
    res.render('admin/adLogin');
}
exports.login = function(req, res) {
    console.log(req.body);
    adminModel.login(req.body).then(function(data) {
        console.log(data);
        if (data.length > 0) {
            req.session.user = data[0]
            console.log(req.session.user);
            res.redirect('/admin')
        } else {
            res.render('admin/adLogin', { errMsg: '用户名密码错误' })
        }
    })
}
exports.loginout = function(req, res) {
    req.session.user = null;
    res.redirect('/admin/login');
}

exports.articleEdit = (req, res) => {
    if (req.params.id == 'favicon.ico') return;
    adminModel.editArtical(req.body).then(data => {
        if (data.affectedRows > 0) {
            res.redirect('/admin/article');
        }
    })
}





//---------用户管理------------
exports.listuser = function(req, res) {
    myGlobal.path = req.originalUrl;
    adminModel.getAllUser(req.query.page).then(function(data) {
        data[0] = utils.arrFormat(data[0]);
        let total = data[1][0].total
        res.render('admin/user', {path:myGlobal.path, user: req.session.user, users: data[0], total: total })
    })
}

exports.addUserPage = function(req, res) {
    res.render('admin/user_add',{path:myGlobal.path, user: req.session.user})
}

exports.addUser = function(req, res) {
    adminModel.getOneUser(req.body).then(function(data) {
        if (data.affectedRows > 0) {
            res.redirect('/admin/listuser')
        }
    })
}

exports.deleteUser = function(req, res) {
    adminModel.deleteUser(req.params.id).then(function(data) {
        if (data.affectedRows > 0) {
            res.redirect('/admin/listuser')
        }
    })
}

exports.getOneuser = function(req, res) {
    adminModel.getOneuser(req.params.id).then(function(data) {
        // console.log(data)
        res.render('admin/user_edit', { oneUser: data[0],path:myGlobal.path, user: req.session.user});
    })
}

exports.editUser = function(req, res) {
    adminModel.editUser(req.body).then(function(data) {
        console.log(data);
        if (data.affectedRows > 0) {
            res.redirect('/admin/listuser')

        }
    })
}


exports.articleDelete = (req, res) => {
    console.log(req.params.id);

    adminModel.deleteArtical(req.params.id).then(data => {
        if (data.affectedRows > 0) {
            res.redirect('/admin/article');
        }
    })
}


exports.search = function(req, res) {
    if (req.body.search.length == 0) return res.json({ result: '空数据' })
    adminModel.search(req.body.search).then(function(data) {
        console.log(data)
        res.json(data)
    })
}